/**
 * project: finance-manage
 * package: com.example.controllers
 * 逻辑处理基类
 */

package com.example.controllers;

import com.example.dataAccess.Connect;
import com.example.models.JsonModel;
import com.example.util.ReadConfig;

import java.io.IOException;
import java.sql.SQLException;

public abstract class Control_Base {
    /* 数据库连接类 */
    protected Connect connect;
    /* 错误码字典 */
    protected JsonModel errdict;

    protected Control_Base() {
        /* 实例化数据库连接类 */
        try {
            connect = new Connect();
        } catch (IOException e) {
            System.out.println("配置文件读取失败");
        } catch (SQLException e) {
            System.out.println("数据库连接失败");
        }

        /* 载入错误码字典 */
        try {
            ReadConfig readConfig = new ReadConfig();
            errdict = readConfig.getErrCodeDictJson();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /* 验证用户身份 */
    // @Override
    protected boolean verifyUser(String username, String personalIdentificationCode) {
        if (personalIdentificationCode != null) {
            String userPwdHash = null;
            try {
                userPwdHash = getHashPwd(username);
            } catch (SQLException e) {
                return false;
            }
            return personalIdentificationCode.equals(userPwdHash);
        }
        return false;
    }

    /* 获取用户密码哈希值 */
    protected abstract String getHashPwd(String username) throws SQLException;

    /* 从错误码字典中获取错误信息 */
    protected JsonModel getErrno(int errno) {
        return (JsonModel) errdict.get(String.valueOf(errno));
    }

    public abstract void close();
}
